package com.zhyonk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhyonk.domain.AjaxResult;
import com.zhyonk.domain.ItemResultInfo;
import com.zhyonk.domain.ItemListInfo;
import com.zhyonk.domain.ItemQueryInfo;
import com.zhyonk.service.TestCommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author cbl
 * @description com.zhyonk.controller
 * @date 2020/7/13
 * 通用页面功能请求
 */
@RestController("/common")
public class TestCommController {

    @Autowired
    private TestCommService testListService;

    /**
     * 测试列表分页查询   根据Id查询信息
     * @param map
     * @return
     */
    @PostMapping("/queryTestInfo")
    public AjaxResult queryTestInfo(@RequestBody Map<String,String> map){
        if (map.isEmpty()){
            return AjaxResult.error("缺少必传参数");
        }
        //flag=list 列表   flag=sing 个位数
        String flag = map.get("flag");
        if ("list".equals(flag)){
            Page<ItemListInfo> testInfos = testListService.queryTestAll(map);
            return AjaxResult.success(testInfos);
        }
        if ("sing".equals(flag)){
            Integer testId = Integer.getInteger(map.get("testId"));
            if (testId == null){
                return AjaxResult.error("缺少testId参数");
            }
            ItemListInfo testInfo = testListService.queryTestById(testId);
            return AjaxResult.success(testInfo);
        }
        return AjaxResult.error("所传参数有误");
    }

    /**
     * 通用测试主题页、结果页所需参数返回
     * @param map
     * @return
     */
    @PostMapping("/queryComm")
    public AjaxResult queryCommInfo(@RequestBody Map<String,String> map){
        if (map.isEmpty()){
            return AjaxResult.error("缺少必传参数");
        }
        //flag=list 列表   flag=sing 个位数
        String flag = map.get("flag");
        if ("title".equals(flag)){
            //查询条件有待完善
            String currTestId = map.get("currTestId");
            if (null == currTestId || "".equals(currTestId)){
                return AjaxResult.error("缺少startTestBtnApi参数");
            }
            ItemQueryInfo titleInfo = testListService.queryTitleAll(currTestId);
            return AjaxResult.success(titleInfo);
        }
        if ("result".equals(flag)){
            String startTestBtnApi = map.get("startTestBtnApi");
            if (null == startTestBtnApi || "".equals(startTestBtnApi)){
                return AjaxResult.error("缺少startTestBtnApi参数");
            }
            ItemResultInfo resultInfo = testListService.queryResultInfo(startTestBtnApi);
            return AjaxResult.success(resultInfo);
        }
        return AjaxResult.error("所传参数有误");
    }
}
